package model;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * A data access object (DAO) providing persistence and search support for
 * Coursetype entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see model.Coursetype
 * @author MyEclipse Persistence Tools
 */

public class CoursetypeDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(CoursetypeDAO.class);
	// property constants
	public static final String COURSETYPE = "coursetype";

	protected void initDao() {
		// do nothing
	}

	public void deleteAllCoursetypeInfo()
	{
		String hql="DELETE FROM Coursetype AS ct";
		Query q=super.getSession().createQuery(hql);
		q.executeUpdate();
	}
	
	//按课程类别搜索课程类别信息
	public List searchCoursetypes(String coursetype)
	{
		String hql="FROM Coursetype AS ct";
		String temp="WHERE ";
		String clause=temp;
		
		if(coursetype!=null&&!"".equals(coursetype))
		{
			clause=clause+"ct.coursetype like :coursetype";
		}
		
		if(!clause.equals(temp))
		{
			hql=hql+" "+clause;
		}
		System.out.println(hql);
		Query q=super.getSession().createQuery(hql);
		if(coursetype!=null&&!"".equals(coursetype))
		{
			q.setString("coursetype", '%'+coursetype+'%');
		}
		
		System.out.println(hql);
		List searchlist=q.list();
		return searchlist;
	}
	
	public void save(Coursetype transientInstance) {
		log.debug("saving Coursetype instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Coursetype persistentInstance) {
		log.debug("deleting Coursetype instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Coursetype findById(java.lang.Integer id) {
		log.debug("getting Coursetype instance with id: " + id);
		try {
			Coursetype instance = (Coursetype) getHibernateTemplate().get(
					"model.Coursetype", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Coursetype instance) {
		log.debug("finding Coursetype instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Coursetype instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Coursetype as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByCoursetype(Object coursetype) {
		return findByProperty(COURSETYPE, coursetype);
	}

	public List findAll() {
		log.debug("finding all Coursetype instances");
		try {
			String queryString = "from Coursetype";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Coursetype merge(Coursetype detachedInstance) {
		log.debug("merging Coursetype instance");
		try {
			Coursetype result = (Coursetype) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Coursetype instance) {
		log.debug("attaching dirty Coursetype instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Coursetype instance) {
		log.debug("attaching clean Coursetype instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static CoursetypeDAO getFromApplicationContext(ApplicationContext ctx) {
		return (CoursetypeDAO) ctx.getBean("CoursetypeDAO");
	}
}